#include "stdafx.h"
#include "libVideoSaver/cVideoSaveFFMsvc.h"
#include "libVideoSaver/libvideo_saver_ff.h"

#include <string.h>
#include <stdio.h>

cVideoSaveFFMsvc::~cVideoSaveFFMsvc()
{
    Uninit();
}

int cVideoSaveFFMsvc::Init()
{
    Uninit();
    m_pHandle = Video_CreateProcessHandle();
    if (m_pHandle == NULL)
    {
        printf("Video_CreateProcessHandle failed\n");
        return -1;
    }
    return 0;
}

int cVideoSaveFFMsvc::Uninit()
{
    if (m_pHandle != NULL)
    {
        Video_CloseVideoFile(m_pHandle);
        m_pHandle = NULL;
    }    
    return 0;
}

int cVideoSaveFFMsvc::CreateVideoFile(const char *chFileName, int iVideoWidth, int iVideoHeight, int iFrameRate)
{
    if (m_pHandle == NULL)
    {
        printf("m_pHandle is NULL\n");
        return -1;
    }
    if (iVideoWidth <= 0 || iVideoHeight <= 0 || iFrameRate <= 0)
    {
        printf("iVideoWidth=%d, iVideoHeight=%d, iFrameRate=%d\n", iVideoWidth, iVideoHeight, iFrameRate);
        return -2;
    }
    int iRet = Video_CreateVideoFile(m_pHandle, chFileName, iVideoWidth, iVideoHeight, iFrameRate);
    return iRet;
}

int cVideoSaveFFMsvc::WriteH264Frame(bool bIframe, unsigned char *pbFrameData, int iFrameSize)
{
    if (m_pHandle == NULL|| pbFrameData == NULL || iFrameSize <= 0)
    {
        printf("m_pHandle=%p, pbFrameData=%p, iFrameSize=%d\n", m_pHandle, pbFrameData, iFrameSize);
        return -1;
    }
    int iRet = Video_WriteH264Frame(m_pHandle, bIframe ? 1 : 0, pbFrameData, iFrameSize);
    return iRet;
}

int cVideoSaveFFMsvc::CloseVideoFile()
{
    if (m_pHandle == NULL)
    {
        printf("m_pHandle is NULL\n");
        return -1;
    }
    int iRet = Video_CloseVideoFile(m_pHandle);
    return iRet;
}
